//
//  IGIssueImageCollectionView.m
//  jingdongfang
//
//  Created by BOE on 2017/8/16.
//  Copyright © 2017年 kevinLY. All rights reserved.
//

#import "IGIssueImageCollectionView.h"
#import "IGIssueImageCell.h"
#import "IGIssueDelImageViewController.h"

static NSString *issueImageCellIdentifier = @"issueImageCellIdentifier";
@interface IGIssueImageCollectionView()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

/** 选择的照片数组 */
@property (nonatomic, strong) NSMutableArray *selectedAssets;

@end

@implementation IGIssueImageCollectionView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addSubview:self.collectionView];
        [self addNotification];
        
    }
    return self;
}

- (void)addNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectedImageDelNotification:) name:@"selectedImageDelNotification" object:nil];
}

- (void)layoutSubviews
{
    self.collectionView.frame = CGRectMake(0, 6, self.width, self.height - 12);
}

#pragma mark - 懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 15, 0);
        flowLayout.minimumLineSpacing = lineSpaceWidthImage;
        flowLayout.minimumInteritemSpacing = lineSpaceWidthImage;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass:[IGIssueImageCell class] forCellWithReuseIdentifier:issueImageCellIdentifier];
        
    }
    return _collectionView;
}

- (NSMutableArray *)selectedAssets
{
    if (!_selectedAssets) {
        _selectedAssets = [[NSMutableArray alloc] init];
    }
    return _selectedAssets;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.selectedAssets.count < 9) {
        return self.selectedAssets.count + 1;
    }
    return self.selectedAssets.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGIssueImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:issueImageCellIdentifier forIndexPath:indexPath];
    
    if (self.selectedAssets.count < 9) {
        
        if (indexPath.item == self.selectedAssets.count) {
//            cell.imageView.image = [UIImage imageNamed:@"upload_image"];
            cell.imageView.image = [UIImage imageNamed:@"uploadImage"];
        } else {
            cell.imageView.image = self.selectedAssets[indexPath.item];
        }
    } else {
        cell.imageView.image = self.selectedAssets[indexPath.item];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.selectedAssets.count) {
        __weak typeof(self) weakSelf = self;
        [IGPermissionUtil ig_showActionSheetWithCameraBlock:^{
            [IGTakePhotosUtil ig_showImagePickerControllerWithImagePickerBlock:^ (NSDictionary<NSString *,id> * _Nullable info){
                if (info) {
                    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
                    image = [image fixOrientation];
                    [weakSelf.selectedAssets addObjectsFromArray:@[image]];
                    [weakSelf reloadCollectionView];
                }
            }];
        } photoBlock:^{
            int maximumNumberOfSelection = (int)(9 - weakSelf.selectedAssets.count);
            [IGTakePhotosUtil ig_showAssetsPickerControllerWithMaximumNumberOfSelection:maximumNumberOfSelection assetsPickerBlock:^(NSArray * _Nonnull assets) {
                if (assets.count != 0) {
                    [weakSelf.selectedAssets addObjectsFromArray:assets];
                    [weakSelf reloadCollectionView];
                }
            }];
        }];
        
    }else{
        //删除图片操作
        IGIssueDelImageViewController *vc = [[IGIssueDelImageViewController alloc] initWithAssets:_selectedAssets andImageIndexPath:indexPath];
        [UIViewController ig_pushViewController:vc animated:YES];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionViewLayout;
    CGFloat width = (self.collectionView.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right - flowLayout.minimumLineSpacing * 3) / 4;
    return CGSizeMake(width, width);
}

#pragma mark - help
- (void)selectedImageDelNotification:(NSNotification *)notification {
    NSDictionary *userInfo = notification.userInfo;
    
    [_selectedAssets removeAllObjects];
    [_selectedAssets addObjectsFromArray:[userInfo objectForKey:@"assets"]];
    
    [self reloadCollectionView];
}

- (void)reloadCollectionView
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"imageCollectionViewReloadNotifaction" object:self.selectedAssets];
    [self.collectionView reloadData];
}

+ (CGFloat)getCellItemWidth
{
    return (KSCREENWIDTH - 20 - lineSpaceWidthImage * 3)/4;
}

#pragma mark - dealloc
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"selectedImageDelNotification" object:nil];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
